package Thread;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {
	public static final int PORT = 5678;

	public static void main(String[] args) {
		Client c = new Client();
		c.run();
	}

	public void run() {
		try {
			// 创建socket套接字，连接本机的PORT端口
			Socket socket = new Socket("127.0.0.1", PORT);
			// 创建缓冲区对象，用于读取服务器发送的信息
			BufferedReader sin = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			// 创建缓冲区对象，用于向服务器发送信息
			OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
			PrintWriter sout = new PrintWriter(osw);
			// 创建缓冲区对象，用于接受控制台输入
			BufferedReader in = new BufferedReader(new InputStreamReader(
					System.in));
			// 从控制台读取一行信息
			String message = in.readLine();
			// 向服务器发送请求
			while (message != null && !"bye".equals(message)) {
				sout.println(message);
				sout.flush();
				System.out.println("Server answer: " + sin.readLine());
				message = in.readLine();
			}
			sin.close();
			sout.close();
			osw.close();
			socket.close();
		} catch (Exception e) {
			System.out.print("客户端出错，信息如下：\n" + e.getMessage());
		}
	}

}
